<?php
require_once(dirname(__FILE__)."/../lib/Loader.php");
cfgload("defines");
cfgload("strings/kinoItunesList");

cload("Smarty.smartyRenderer");
cload("requestProcessor");
cload("kinopoisk.kinoPoiskFilm");
cload("itunes.itunesRow");
/**
 * Description of kinoItunesForm
 *
 * @author Di_Moon
 */
class kinoItunesForm extends smartyRenderer {
    protected $_dbh;
    protected $_kpId;
    protected $_countryCode;
    protected $_kpFilm;
    
    function __construct($dbh) {
        parent::__construct();
        
        $this->_dbh = $dbh;
        
        $this->_kpId = (int)requestProcessor::getVar("kpid");
        $this->_countryCode = requestProcessor::getVar("country");
        $this->_kpFilm = new kinoPoiskFilm($this->_dbh, $this->_kpId);
        
        $this->assign("kpid", $this->_kpId);
        $this->assign("countryCode", $this->_countryCode);
    }
    
    function showForm() {
        $row = new itunesRow($this->_dbh, $this->_kpId, $this->_countryCode);
        $row->load();
        
        $jsonData = requestProcessor::getVar("json", "");
        
        // если есть json данные, то row заполним из них
        if($jsonData != "") {
            $t_arr = json_decode($_POST["json"]);
            $t_data = $t_arr[0];
            $row->set("trackName",          htmlspecialchars(@$t_data->trackName));
            $row->set("trackCensoredName",  htmlspecialchars(@$t_data->trackCensoredName));
            $row->set("trackId",            htmlspecialchars(@$t_data->trackId));
            $row->set("artistName",         htmlspecialchars(@$t_data->artistName));
            $row->set("trackViewUrl",       htmlspecialchars(@$t_data->trackViewUrl));
            $row->set("artworkUrl30",       htmlspecialchars(@$t_data->artworkUrl30));
            $row->set("releaseDate",        htmlspecialchars(@$t_data->releaseDate));
            $row->set("primaryGenreName",   htmlspecialchars(@$t_data->primaryGenreName));
            $row->set("contentAdvisoryRating", htmlspecialchars(@$t_data->contentAdvisoryRating));
            $row->set("shortDescription",   htmlspecialchars(@$t_data->shortDescription));
            $row->set("longDescription",    htmlspecialchars(@$t_data->longDescription));
            $row->set("Studio",             "n/a");
            $row->set("RatingAnnotation",   "n/a");
            $row->set("Copyright",          "n/a");
            $row->set("XID",                "Apple:vendor_id:".$this->_kpFilm->get("year")."-".$this->_kpId);
        }
        
        $this->assign("itunes", $row->getElements());
        
        return $this->fetch("kinoItunesForm.tpl");
    }
    
    function showSendResult() {
        $_post = requestProcessor::post();
        $_post->del("kpid");
        $_post->del("country");
        $_post->del("action");
        $row = new itunesRow($this->_dbh, $this->_kpId, $this->_countryCode);
        $row->load();
        
        $row->setElements($_post);
        $row->save();
        
        return $this->fetch("kinoItunesFormResult.tpl");
    }
    
    function render() {
        // Проверим, есть ли фильм
        if(!$this->_kpFilm->filmPresent()) {
            return $this->getError(strings_kinoItunesList::string("FILM_NOT_FOUND"), TPL_ERR_FATAL);
        }
        
        if(!array_key_exists($this->_countryCode, _itunes_country_codes())) {
            return $this->getError(strings_kinoItunesList::string("INVALID_COUNTRYCODE"), TPL_ERR_FATAL);
        }
        
        $_action = requestProcessor::getVar("action", "");
        
        if($_action=="send") return $this->showSendResult();
        
        return $this->showForm();
    }
}

?>
